package com.yixing.tech.entity.dao;

import com.yixing.tech.entity.po.ObjModel;
import com.yixing.tech.common.utils.DataUtil;
import com.yixing.tech.common.vo.s5.ActionVO;
import com.yixing.tech.common.vo.s5.EventVO;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.*;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yixing tech
 * * @date 2022/1/5
 */
@Repository
public class ObjModelDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    public void addAction(ActionVO vo) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(vo.getModelId()));
        Update update = new Update();
        ObjModel.Action action = DataUtil.o2o(vo, ObjModel.Action.class);
        update.addToSet("actions", action);
        mongoTemplate.upsert(query, update, ObjModel.class);
    }

    public void addEvent(EventVO vo) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(vo.getModelId()));
        Update update = new Update();
        ObjModel.Event event = DataUtil.o2o(vo, ObjModel.Event.class);
        update.addToSet("events", event);
        mongoTemplate.upsert(query, update, ObjModel.class);
    }

    public ObjModel getManualAction(String productId) {
        Query query = new Query();
//        query.addCriteria(Criteria.where("productId").is(productId).and("buttons.type").is(ActionType.option.name()));
        query.addCriteria(Criteria.where("productId").is(productId));
        return mongoTemplate.findOne(query, ObjModel.class);
    }

    public ActionVO getAction(String productId, String funCode, String code) {
        List<AggregationOperation> commonOperations = new ArrayList<>();
        MatchOperation match = Aggregation.match(
                Criteria.where("productId").is(productId));
        commonOperations.add(match);
        ProjectionOperation project = Aggregation.project("actions");
        commonOperations.add(project);
        UnwindOperation unwind = Aggregation.unwind("actions");
        commonOperations.add(unwind);
        MatchOperation match1 = Aggregation.match(
                Criteria.where("actions.code").is(code).and("actions.funCode").is(funCode));
        commonOperations.add(match1);
        Aggregation aggregation = Aggregation.newAggregation(commonOperations);
        AggregationResults<ObjModel> reminds = mongoTemplate.aggregate(aggregation, "objModel", ObjModel.class);
        ArrayList<Document> list = reminds.getRawResults().get("results", ArrayList.class);
        if (list == null || list.isEmpty()) return null;
        Object jo = list.get(0).get("actions");
        ActionVO vo = DataUtil.o2o(jo, ActionVO.class);
        return vo;
    }

}
